package com.gxa.p2p.service.impl;

import com.gxa.p2p.common.enum1.ExceptionCode;
import com.gxa.p2p.common.exception.MyException;
import com.gxa.p2p.mapper.VersionMapper;
import com.gxa.p2p.pojo.Version;
import com.gxa.p2p.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service("versionService")
public class VersionServiceImpl implements VersionService {
    @Autowired
    private VersionMapper versionMapper=null;
    @Override
    public List<Version> selectAll(Boolean vstate, Integer page, Integer size) {
        List<Version> maps = versionMapper.selectAll(vstate, null,(page-1)*size, size);
        return maps;
    }

    @Override
    public void add(Version version) throws MyException {
        if(version.getVid()==null){
            version.setVstate(true);
            versionMapper.add(version);
        }else{
            throw new MyException(ExceptionCode.XIEYI_EXIST);
        }
    }

    @Override
    public void update(Long vid, Boolean vstate) throws MyException {
        Version version = versionMapper.selectById(vid);
        if(version!=null){
            if(!version.getVstate().toString().equals(vstate.toString())){
                versionMapper.update(vid, vstate);
            }else{
                throw new MyException(ExceptionCode.STATE_EXIST);
            }
        }else{
            throw new MyException(ExceptionCode.NO_ID);
        }
    }

    @Override
    public void deleteById(Long vid) throws MyException {
        Version version = versionMapper.selectById(vid);
        if(version!=null){
            versionMapper.deleteById(vid);
        }else{
            throw new MyException(ExceptionCode.NO_PERSON);
        }
    }

    @Override
    public List<Version> selectAllData() {
        List<Version> versions = versionMapper.selectAllData();
        return versions;

    }
}
